package com.huaxin.hxcloud.activity;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidu.mapapi.map.Text;
import com.huaxin.common.BaseActivity;
import com.huaxin.common.Constants;
import com.huaxin.common.utils.HxAppClearMgr;
import com.huaxin.common.utils.PrefUtil;
import com.huaxin.common.utils.ToastUtil;
import com.huaxin.common.view.ToggleButton;
import com.huaxin.hxcloud.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SettingNotificationActivity extends BaseActivity {


    //    @BindView(R.id.more)
    //    ImageView more;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    //    @BindView(R.id.tb_accept)
    //    ToggleButton tbAccept;
    //    @BindView(R.id.rl_accept)
    //    RelativeLayout rlAccept;
    //    @BindView(R.id.tb_voice)
    //    ToggleButton tbVoice;
    //    @BindView(R.id.rl_voice)
    //    RelativeLayout rlVoice;
    //    @BindView(R.id.tb_vibration)
    //    ToggleButton tbVibration;
    //    @BindView(R.id.rl_vibration)
    //    RelativeLayout rlVibration;
    //    @BindView(R.id.tb_app_exit)
    //    ToggleButton tbAppExit;
    //    @BindView(R.id.rl_app_exit)
    //    RelativeLayout rlAppExit;

    @BindView(R.id.tb_save_pw)
    ToggleButton   tbSavePw;
    //    @BindView(R.id.rl_save_pw)
    //    RelativeLayout rlSavePw;
    @BindView(R.id.tb_auto_login)
    ToggleButton   tbAutoLogin;
    @BindView(R.id.rl_clean_cache)
    RelativeLayout mRlCleanCache;
    @BindView(R.id.tb_app_exit)
    ToggleButton   mTbAppExit;
    @BindView(R.id.tv_cache_size)
    TextView       mTvCacheSize;
    //    @BindView(R.id.rl_auto_login)
    //    RelativeLayout rlAutoLogin;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_setting_notifcation;
    }

    @Override
    public void initView() {
        setSupportActionBar(toolbar);
        toolbar.setTitle("消息通知");
        //        setToggleChanged(tbAccept, Constants.PK_NOTIFICATION_ACCEPT);
        //        setToggleChanged(tbVoice, Constants.PK_NOTIFICATION_SOUND);
        //        setToggleChanged(tbVibration, Constants.PK_NOTIFICATION_VIBRATION);
        //        setToggleChanged(tbAppExit, Constants.PK_NOTIFICATION_DISABLE_WHEN_EXIT);

        //恢复两个toggleButton的选择状态
        if (PrefUtil.get(Constants.PK_SAVE_PW, false)) {
            tbSavePw.setToggleOn();
        } else {
            tbSavePw.setToggleOff();
        }
        if (PrefUtil.get(Constants.PK_AUTO_LOGIN, false)) {
            tbAutoLogin.setToggleOn();
        } else {
            tbAutoLogin.setToggleOff();
        }
        tbSavePw.setOnToggleChanged(new ToggleButton.OnToggleChanged() {
            @Override
            public void onToggle(boolean on) {
                if (!on) {
                    tbAutoLogin.setToggleOff();
                }
                PrefUtil.set(Constants.PK_SAVE_PW, on);
            }
        });
        tbAutoLogin.setOnToggleChanged(new ToggleButton.OnToggleChanged() {
            @Override
            public void onToggle(boolean on) {
                if (on) {
                    tbSavePw.setToggleOn();
                }
                PrefUtil.set(Constants.PK_AUTO_LOGIN, on);
            }
        });
        mTvCacheSize.setText(HxAppClearMgr.getAppClearSize(this));
    }

    public void initData() {
        //        setToggle(PrefUtil.get(Constants.PK_NOTIFICATION_ACCEPT, true), tbAccept);
        //        setToggle(PrefUtil.get(Constants.PK_NOTIFICATION_SOUND, true), tbVoice);
        //        setToggle(PrefUtil.get(Constants.PK_NOTIFICATION_VIBRATION, true), tbVibration);
        //        setToggle(PrefUtil.get(Constants.PK_NOTIFICATION_DISABLE_WHEN_EXIT, false), tbAppExit);
    }

    private void setToggleChanged(ToggleButton tb, final String key) {
        tb.setOnToggleChanged(new ToggleButton.OnToggleChanged() {

            @Override
            public void onToggle(boolean on) {
                PrefUtil.set(key, on);
            }
        });
    }

    private void setToggle(boolean value, ToggleButton tb) {
        if (value)
            tb.setToggleOn();
        else
            tb.setToggleOff();
    }


    @OnClick({R.id.rl_clean_cache})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_clean_cache:
                if ("0.0Byte".equals(HxAppClearMgr.getAppClearSize(this))) {
                    return;
                }
                HxAppClearMgr.cleanApplicationData(this);
                mTvCacheSize.setText(HxAppClearMgr.getAppClearSize(this));
                ToastUtil.showToast("清理成功");
                break;
        }
    }
}
